package com.sinaapp.zygl.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sinaapp.zygl.bean.User;
import com.sinaapp.zygl.dao.UserDao;
import com.sinaapp.zygl.dao.impl.UserDaoImpl;
import com.sinaapp.zygl.util.JsonUtil;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = -2255203394567836741L;

	@Override
	public String execute() throws Exception {
		UserDao dao = new UserDaoImpl();
		User u = new User();
		u.setName(username);
		u.setPwd(userpwd);
		u.setType(usertype);
		User loginUser = dao.login(u);
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		if (loginUser != null) {
			session.put("username", username);
			session.put("user", loginUser);
			session.put("success", true);
			JsonUtil.writeJson(session);
			return null;
		} else {
			session.put("username", "");
			session.put("success", false);
			JsonUtil.writeJson(session);
		}
		return null;
	}

	/**
	 * 登出
	 * @return
	 * @throws Exception
	 */
	public String loginOut() throws Exception {
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		session.clear();
		return SUCCESS;
	}

	private String username;
	private String userpwd;
	private String usertype;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUserpwd() {
		return userpwd;
	}

	public void setUserpwd(String userpwd) {
		this.userpwd = userpwd;
	}

	public String getUsertype() {
		return usertype;
	}

	public void setUsertype(String usertype) {
		this.usertype = usertype;
	}

}
